<?php

namespace App\Http\Controllers\Admin;



use App\Http\Requests\VideoCategory\StoreVideoCategoryRequest;
use App\Http\Requests\VideoCategory\UpdateVideoCategoryRequest;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Http\Requests;
use App\Model\VideoCategory as Category;


class VideoCategoryController extends Controller
{
    /**
     * 构造函数
     * CategoryController constructor.
     */
    public function __construct()
    {
        parent::__construct();
        view()->share([
            '_xHotel' => 'am-in',
            'categories' => Category::get_categories(),
            '_video_category' => 'am-active',
        ]);
    }

    /**
     * 首页
     * @return mixed
     */
    public function index()
    {
        
        return view('Admin.xHotel.videocategory.index');

    }
    
    /**
     * 新增显示页
     */
    public function create()
    {

        return view('Admin.xHotel.videocategory.create');

    }

    /**
     * 新增
     * @param StoreVideoCategoryRequest $request
     */
    public function store(StoreVideoCategoryRequest $request)
    {
        Category::create($request->all());
        Category::clear();
        return redirect('/videoCategory')->with('success', '添加成功');
    }

    /**
     * 修改显示页
     * @param $id
     */
    public function edit($id)
    {
        $data=array();
        $category = Category::find($id);

        $is_parent = Category::where('id', $category['parent_id'])->get()->toArray();
        $data['data'] = empty($is_parent)? true : false;
        Category::clear();
        return view('Admin.xHotel.videocategory.edit')->with('category',$category)->with('data',$data);;
    }

    /**
     * 修改
     * @param UpdateVideoCategoryRequest $request
     * @param $id
     */
    public function update(UpdateVideoCategoryRequest $request, $id)
    {
        $category = Category::find($id);
        $category->update($request->all());
        Category::clear();
        return redirect('/videoCategory')->with('success', '编辑成功');
    }

    /**
     * 删除
     * @param $id
     */
    public function destroy($id)
    {

        if (!Category::check_children($id)) {
            return back()->with('error', '当前分类有子分类，请先将子分类删除后再尝试删除~');
        }

        if (!Category::is_hotel($id)) {
            return back()->with('error', '当前分类有视频，请先将视频删除后再尝试删除~');
        }

        Category::destroy($id);
        Category::clear();
        return back()->with('success', '删除成功');

    }

    /**
     * 是否显示
     * @param Request $request
     */
    public function is_something(Request $request){
        $attr = $request->attr;
        $category = Category::find($request->id);
        $value = $category->$attr ? false : true;
        $category->$attr = $value;
        Category::clear();
        $category->save();
    }

    /**
     * 排序
     * @param Request $request
     */
    public function sort_order(Request $request){
        $sort_order=$request->sort_order;
        $category = Category::find($request->id);
        $category->sort_order=$sort_order;
        Category::clear();
        $category->save();
    }


}
